//
//  GYHomeBuyListVC.m
//  GYDemo
//
//  Created by Mrzhang on 2018/4/2.
//  Copyright © 2018年 mac. All rights reserved.
//

//
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                  \\`.
//        .'//                     |                     \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-. \\`.
//    .'//.-"                 `-.  |  .-'                 "-.\\`.
//  .'//______.============-..   \ | /   ..-============.______\\`.
//.'______________________________\|/______________________________`.
//
//

#import "GYHomeBuyListVC.h"
#import "Head.h"
#import "GYHomeBuyViewModel.h"
#import "GYHomeBuyDetailVC.h"
@interface GYHomeBuyListVC ()
{
    NSInteger currentPage;
}
@property(nonatomic,strong)GYHomeBuyViewModel* buyViewModel;
@property(nonatomic,strong)GYChooseView* chooseView;
@property(nonatomic,strong)NSMutableDictionary* paraDataSource;
@end

@implementation GYHomeBuyListVC
#pragma mark - getter and setter
-(GYHomeBuyViewModel *)buyViewModel
{
    if (!_buyViewModel) {
        _buyViewModel=[[GYHomeBuyViewModel alloc] init];
    }
    return _buyViewModel;
}
-(NSMutableDictionary *)paraDataSource{
    
    if (!_paraDataSource) {
        _paraDataSource=[[NSMutableDictionary alloc] init];
    }
    return _paraDataSource;
}
-(GYChooseView *)chooseView
{
    if (!_chooseView) {
        _chooseView=[[GYChooseView alloc] initWithFrame:WFCGRectMake(0, 0, 375, 45) chooseViewType:ChooseViewTypeAllNoRegin];
        _chooseView.backgroundColor=[UIColor whiteColor];
        
        [WFFunctions WFUIaddBorderToView:_chooseView top:NO left:0 bottom:1 right:0 borderColor:RGBACOLOR(240, 240, 240, 1) borderWidth:0.5];
        @WeakSelf;
        _chooseView.chooseBack = ^(NSDictionary *paraDic) {
            for (NSString* key in paraDic.allKeys)
            {
                [selfp.paraDataSource setObject:paraDic[key] forKey:key];
            }
            currentPage=1;
            [selfp loadData];
        };
        _chooseView.categoryback = ^(NSString *categoryStr) {
            currentPage=1;
            [selfp.paraDataSource setObject:categoryStr forKey:@"sort"];
            [selfp setOriginData];
            [selfp loadData];
            
        };
    }
    return _chooseView;
}

-(void)initilization
{
    self.automaticallyAdjustsScrollViewInsets = NO;


    self.baseTable.showsVerticalScrollIndicator=NO;
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    [self.view addSubview:self.baseTable];
    [self.baseTable setBackgroundColor:[UIColor whiteColor]];
    
    
    [self setNavgationTheme:0];
   // [self.view addSubview:self.chooseView];
    [self.view addSubview:self.baseTable];
    self.baseTable.y=WFCGFloatY(0);
    [self.baseTable setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [self.baseTable setSeparatorColor: RGBACOLOR(233, 233, 233, 1)];
    [self.baseTable setBackgroundColor:Main_Color_BG];
    [self.baseTable setTintColor:Main_Color_BG];
    self.baseTable.showsVerticalScrollIndicator=NO;
    self.baseTable.height = self.view.height - GY_Nav_height-WFCGFloatY(0)-self.bottomShelterHeight;
    
    @WeakSelf;
    self.baseTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        currentPage = 1;
        [selfp setOriginData];
        [selfp loadData];
    }];
    self.baseTable.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
        if (selfp.buyViewModel.canAddMore)
        {
            currentPage += 1;
            [selfp.paraDataSource setObject:[NSString stringWithFormat:@"%ld",currentPage] forKey:@"page"];
            [selfp loadData];
        }else
        {
            [selfp.baseTable.mj_footer endRefreshingWithNoMoreData];
        }
        
    }];

    [self loadData];
}

-(void)viewModelBand
{
    @WeakSelf;
    
    GCBaseObservationModel* buyOber=[GCBaseObservationModel new];
    buyOber.keyPath=@"datas";
    buyOber.observation=self.buyViewModel;
    buyOber.handler = ^(NSString *keyPath) {
        [selfp.baseTable reloadData];
        if ([selfp.paraDataSource[@"page"] isEqualToString:@"1"]) {
            [selfp.baseTable scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionNone animated:YES];
        }
    };
    [self registObservation:buyOber];
    
    
    GCBaseObservationModel *observation1 = [GCBaseObservationModel new];
    observation1.observation = self.buyViewModel;
    observation1.keyPath = @"totalCount";
    observation1.handler = ^(NSString *keyPath) {
        [selfp.chooseView setTotalCount:selfp.buyViewModel.totalCount];
    };
    [self registObservation:observation1];

}
-(void)setOriginData
{
    currentPage = 1;
    for (NSString* key in self.paraDataSource.allKeys) {
        if (![key isEqualToString:@"sort"])
        {
            [self.paraDataSource removeObjectForKey:key];
        }
    }
    self.paraDataSource[@"page"]=@"1";
    self.paraDataSource[@"pagesize"]=@"10";
}
-(void)loadData
{
//    self.paraDataSource[@"pagesize"]=@"10";
//    self.paraDataSource[@"page"]=[NSString stringWithFormat:@"%ld",currentPage];
//    [self.buyViewModel requestListDatas:self.paraDataSource];
    [self.buyViewModel requestDatas:[NSMutableDictionary new]];
}
#pragma mark - life
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.isGroup=YES;
    currentPage = 1;

    [self viewModelBand];
    [self initilization];
    self.title=@"求购信息";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - delegate
#pragma mark - table delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.buyViewModel.datas.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *CellID = @"CellIDThird";
    GYBuyListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
    if (cell == nil) {
        cell = [[GYBuyListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        // @WeakSelf
    }
    GYHomeBuyProductInfoModel *model = self.buyViewModel.datas[indexPath.row];
    cell.model = model;
    
    if ([[GYLoginViewModel shareInterface] roleType]==1)
    {
        @WeakSelf;
        [cell.offerBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
            GYHomeBuyDetailVC* gy=[[GYHomeBuyDetailVC alloc] init];
            gy.passID=[NSString stringWithFormat:@"%@",model.id];
            gy.passModel=model;
            [selfp.navigationController pushViewController:gy animated:YES];
        }];
    }else
    {
        [cell.offerBtn setHidden:YES];
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return WFCGFloatY(110);
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0001f;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView* view=[UIView new];
    view.backgroundColor=Main_Color_BGLightGray;
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return .001f;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    GYHomeBuyProductInfoModel *model = self.buyViewModel.datas[indexPath.row];
  
    GYHomeBuyDetailVC* gy=[[GYHomeBuyDetailVC alloc]init];
    gy.passID=[NSString stringWithFormat:@"%@",model.id];
    gy.passModel=model;
    [self.navigationController pushViewController:gy animated:YES];
}
#pragma scrolldelegate
@end


/*
 11111111111111111111111111111111111111001111111111111111111111111
 11111111111111111111111111111111111100011111111111111111111111111
 11111111111111111111111111111111100001111111111111111111111111111
 11111111111111111111111111111110000111111111111111111111111111111
 11111111111111111111111111111000000111111111111111111111111111111
 11111111111111111111111111100000011110001100000000000000011111111
 11111111111111111100000000000000000000000000000000011111111111111
 11111111111111110111000000000000000000000000000011111111111111111
 11111111111111111111111000000000000000000000000000000000111111111
 11111111111111111110000000000000000000000000000000111111111111111
 11111111111111111100011100000000000000000000000000000111111111111
 11111111111111100000110000000000011000000000000000000011111111111
 11111111111111000000000000000100111100000000000001100000111111111
 11111111110000000000000000001110111110000000000000111000011111111
 11111111000000000000000000011111111100000000000000011110001111111
 11111110000000011111111111111111111100000000000000001111100111111
 11111111000001111111111111111111110000000000000000001111111111111
 11111111110111111111111111111100000000000000000000000111111111111
 11111111111111110000000000000000000000000000000000000111111111111
 11111111111111111100000000000000000000000000001100000111111111111
 11111111111111000000000000000000000000000000111100000111111111111
 11111111111000000000000000000000000000000001111110000111111111111
 11111111100000000000000000000000000000001111111110000111111111111
 11111110000000000000000000000000000000111111111110000111111111111
 11111100000000000000000001110000001111111111111110001111111111111
 11111000000000000000011111111111111111111111111110011111111111111
 11110000000000000001111111111111111100111111111111111111111111111
 11100000000000000011111111111111111111100001111111111111111111111
 11100000000001000111111111111111111111111000001111111111111111111
 11000000000001100111111111111111111111111110000000111111111111111
 11000000000000111011111111111100011111000011100000001111111111111
 11000000000000011111111111111111000111110000000000000011111111111
 11000000000000000011111111111111000000000000000000000000111111111
 11001000000000000000001111111110000000000000000000000000001111111
 11100110000000000001111111110000000000000000111000000000000111111
 11110110000000000000000000000000000000000111111111110000000011111
 11111110000000000000000000000000000000001111111111111100000001111
 11111110000010000000000000000001100000000111011111111110000001111
 11111111000111110000000000000111110000000000111111111110110000111
 11111110001111111100010000000001111100000111111111111111110000111
 11111110001111111111111110000000111111100000000111111111111000111
 11111111001111111111111111111000000111111111111111111111111100011
 11111111101111111111111111111110000111111111111111111111111001111
 11111111111111111111111111111110001111111111111111111111100111111
 11111111111111111111111111111111001111111111111111111111001111111
 11111111111111111111111111111111100111111111111111111111111111111
 11111111111111111111111111111111110111111111111111111111111111111
 
 
 */
